local choucuo = fk.CreateSkill{
    name = "wk_heg__choucuo",
    tags = { Skill.DeputyPlace },
}

Fk:loadTranslationTable{
    ["wk_heg__choucuo"] = "筹措",
    [":wk_heg__choucuo"] = "副将技，出牌阶段开始时，你可摸两张牌并展示之，然后你于此阶段内不能使用其它手牌直至你失去这些牌，且此阶段结束时，若你于此阶段内使用的牌数不小于体力值，你交换主副将。",

    ["@@wk_heg__choucuo_inhand-phase"] = "筹措",

    ["$wk_heg__choucuo1"] = "丞相新丧，吾当继之。",
    ["$wk_heg__choucuo2"] = "规划分部，筹度粮谷。",
}

local HG = require "packages/new_heg_diy_my/util"

choucuo:addEffect(fk.EventPhaseStart,{
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(choucuo.name) and player.phase == Player.Play
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = player:drawCards(2, choucuo.name)
    player:showCards(cards)
    if not player.dead then
      local mark = {}
      for _, id in ipairs(cards) do
        if room:getCardArea(id) == Card.PlayerHand and room:getCardOwner(id) == player then
          table.insert(mark, id)
          room:setCardMark(Fk:getCardById(id), "@@wk_heg__choucuo_inhand-phase", 1)
        end
      end
      room:setPlayerMark(player, "wk_heg__choucuo-phase", mark)
    end
  end,
})

choucuo:addEffect(fk.AfterCardsMove,{
  can_refresh = function(self, event, target, player, data)
    if type(player:getMark("wk_heg__choucuo-phase")) ~= "table" then return false end
    local mark = player:getMark("wk_heg__choucuo-phase")
    local toLose = {}
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand and table.contains(mark, info.cardId) then
            table.insert(toLose, info.cardId)
          end
        end
      end
    end
    if #toLose > 0 then
      event:setCostData(self,{ cards = toLose })
      return true
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local mark = player:getMark("wk_heg__choucuo-phase")
    local cards = event:getCostData(self).cards
    table.forEach(cards, function(id) table.removeOne(mark, id) end)
    player.room:setPlayerMark(player, "wk_heg__choucuo-phase", #mark > 0 and mark or 0)
  end,
})

choucuo:addEffect(fk.EventPhaseEnd,{
  is_delay_effect = true,
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player:usedSkillTimes(choucuo.name, Player.HistoryPhase) > 0 and player.phase == Player.Play 
    and #player.room.logic:getEventsOfScope(GameEvent.UseCard, 998, function(e)
      local use = e.data
      return use.from == player
    end, Player.HistoryPhase) >= player.hp
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, choucuo.name, "special")
    player:broadcastSkillInvoke(choucuo.name)
    HG.SwapMainAndDeputy(room, player)
  end,
})

choucuo:addEffect("prohibit",{
  prohibit_use = function(self, player, card)
    if player:getMark("wk_heg__choucuo-phase") == 0 then return false end
    local cards = card:isVirtual() and card.subcards or {card.id}
    return table.find(cards, function(id) return Fk:getCardById(id):getMark("@@wk_heg__choucuo_inhand-phase") == 0 end)
  end,
})

return choucuo